package test;

import org.junit.Test;

import sqlengine.Main;

public class CreateIndex {

	static int score = 0;
	
	@Test
	public void cIndex(){
		boolean eCaught = false;
		String query = "create table simple1 (f1 varchar2(10), f2 varchar2(15) primary key);\n";
		String out = null;
		try{
			Main m = new Main();
			out = m.process(query);
			out = m.process("create index in1 on simple1(f1);\n");			
		}catch(Exception e){
			eCaught = true;
		}
		
		if(!eCaught && out != null && out.equals("Index created.\n")){
			score+= Breakdown.cIndex;
			SC.score.append("\n\t index is created correctly - " + Breakdown.cIndex + "/" + Breakdown.cIndex);
		}else{
			SC.score.append("\n\t index isn't created correctly  - 0/" + Breakdown.cIndex);
		}	
	}
	
	@Test
	public void cIndexExceptions(){
		boolean eCaught = false;
		String query = "create table simple1 (f1 varchar2(10), f2 varchar2(15) primary key);\n";
		String out = null;
		try{
			Main m = new Main();
			out = m.process(query);
			out = m.process("create index in1 on simple1(f1);\n");		
			if(out == null || !out.equals("Index created.\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process("create index in1 on simple1(f1);\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query);
			out = m.process("create index in1 on simple1(f1);\n");
			out = m.process("create index in1 on simple1(f2);\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query);
			out = m.process("create index in1 on simple1(f1);\n");
			out = m.process("create index in2 on simple1(f1);\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query);
			out = m.process("create index 1in1 on simple1(f1);\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();
			//---
			m = new Main();
			out = m.process(query);
			out = m.process("create index in1 on simple1(f11);\n");
			if(out == null || !out.equals("Error\n"))
				throw new Exception();
		}catch(Exception e){
			eCaught = true;
		}
		
		if(!eCaught){
			score+= Breakdown.cIndexEx;
			SC.score.append("\n\t creating index checks for errors correctly - " + Breakdown.cIndexEx + "/" + Breakdown.cIndexEx);
		}else{
			SC.score.append("\n\t creating index doesn't check for errors correctly  - 0/" + Breakdown.cIndexEx);
		}	
	}
	
	
}
